<?php

class System_Models_Staff extends Zend_Db_Table_Abstract {
    protected $_name = "staff";
    protected $_primary = "StaffID";

    public function getOrderMax(){
        return 1;
    }
     public function getAllTree(){
        $query = "SELECT * FROM $this->_name ";
        try {
            $data = $this->getAdapter()->fetchAll($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $data;
    }
    public function selectRow($id,$ParentId,$ParentName) {
        if($id) {
            $sql = $this->getAdapter()->select()
                        ->from(array('A' => $this->_name))
                        ->join(array('B' => 'unit'),'A.UnitID = B.UnitID',array('B.Name As ParentName'))
                        ->where('A.StaffID = ?', $id);
            try {
                $rows = $this->getDefaultAdapter()->fetchRow($sql);
            } catch (Exception $e) {
                echo $e->getMessage();
            }
            if($rows['Birthday'] != '0000-00-00') {
                $rows['Birthday'] = ($rows['Birthday'] ? date('d/m/Y',strtotime($rows['Birthday'])) : '');
            }else {
                $rows['Birthday'] = '';
            }
        }else {
            $query = $this->select()->from($this, '')->columns(array('Orders' => new Zend_Db_Expr('MAX(Orders)')));
            $orders = $this->getAdapter()->fetchRow($query);
            $rows = array(  'StaffID'=>''
                            ,'Name'=>''
                            ,'Birthday'=>''
                            ,'Sex'=>''
                            ,'Username'=>''
                            ,'Password'=>''
                            ,'Orders'=>$orders['Orders']+1
                            ,'Status'=>'1'
                            ,'UnitID'=>$ParentId
                            ,'ParentName'=>$ParentName
                            ,'Address' => ''
                            ,'Status' => '1'
                            ,'Email' => ''
                            ,'Mobile' => ''
                        );
        }
        return $rows;
    }
   
    public function deleteRows($nodeList) {
        $where = "$this->_primary IN ($nodeList)";
        try {
            $result = $this->delete($where);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $result;
    }

    public function checkexit($username) {

        $where = $this->getAdapter()->quoteInto("Username =?", $username);
        $query = "SELECT count(*) as Total FROM $this->_name WHERE $where";
        try {
            $result = $this->getAdapter()->fetchRow($query);
        } catch (Exception $e) {
            echo $e->getMessage();
        }
        return $result['Total'];
    }
    

}
